package com.hhkj.study.admin.resource.controller;

import com.hhkj.study.admin.resource.entity.ResourceCourseOffline;
import com.hhkj.study.admin.resource.excel.ResourceCourseOfflineExcelRes;
import com.hhkj.study.admin.resource.model.req.ResourceCourseDocReq;
import com.hhkj.study.admin.resource.model.req.ResourceCourseOfflineReq;
import com.hhkj.study.admin.resource.model.res.ResourceCourseOfflineRes;
import com.hhkj.study.admin.resource.service.biz.ResourceCourseOfflineBizService;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.v3.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.ExcelImportBaseTemplate;
import com.ruoyi.common.valid.VDisable;
import com.ruoyi.common.valid.VEdit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* ResourceCourseOfflineController
*
* @author SERVER
* @since 2024-09-06
*/
@Slf4j
@RestController
@PermissionPrefix("resource:resourceCourseOffline")
@RequestMapping("/admin/resource/course/offline")
public class ResourceCourseOfflineController extends BaseController<ResourceCourseOffline, ResourceCourseOfflineReq, ResourceCourseOfflineRes, ResourceCourseOfflineBizService> {

    /** 上下架 */
    @PreAuthorize("@ss.hasPermi(this, 'edit')")
    @PutMapping("/publish")
    public AjaxResult updatePublishFlag(@Validated(value = {VEdit.class, VDisable.class}) @RequestBody ResourceCourseDocReq req) {
        return toAjax(this.getBizService().updatePublishFlag(req));
    }

    /**
     * 导出
     * @param req
     * @param response
     */
    @PreAuthorize("@ss.hasPermi(this, ':export')")
    @PostMapping("/export")
    public void export(@RequestBody ResourceCourseOfflineReq req, HttpServletResponse response) {
        try {
            getBizService().export(req, response);
        } catch (GlobalException e) {
            throw new GlobalException(e.getMessage());
        } catch (Exception e) {
            log.error("导出文件失败", e);
            throw new GlobalException("系统异常，文件导出失败，请稍后重试");
        }
    }

    /**
     * 导入
     * @return
     */
    @PreAuthorize("@ss.hasPermi(this, ':import')")
    @PostMapping("/import")
    public AjaxResult importOffline(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        return AjaxResult.success(getBizService().importOffline(file, request));
    }

    @Override
    public ExcelImportBaseTemplate getExcelImportBaseTemplate() {
        return new ResourceCourseOfflineExcelRes();
    }
}
